import '../../css/signUp/signUpCSS.css';

var user_password = '';

//username失去焦点事件
window.onUsernameBlur= function onUsernameBlur(){
    let username = $("#username").val();
    checkUsername(username);
}

//password失去焦点事件
window.onPasswordBlur = function onPasswordBlur(){
    let password = $("#password").val();
    checkPassword(password);
}

//rePassword失去焦点事件
window.onRePasswordBlur = function onRePasswordBlur(){
    let rePassword = $("#repassword").val();
    checkRePassword(rePassword);
}

//点击去登陆按钮
window.clickSignIn = function clickSignIn(e){
    e.preventDefault();
    window.location.href = 'signIn.html';
}

//点击注册按钮
window.clickSignUp = function clickSignUp(e){
    e.preventDefault();
    $("#signUp").text("注册中");
    $("#signUp").addClass("disabled");
    let username = $("#username").val();
    let password = $("#password").val();
    let rePassword = $("#repassword").val();
    if(checkUsername(username) && checkPassword(password) && checkRePassword(rePassword)){
        //异步请求注册
        let formDatas = new FormData();
        formDatas.append('username',username);
        formDatas.append('password',password);

        fetch(`http://localhost:8022/user/signUp`,{
            method:'POST',
            mode: 'cors',
            body:formDatas

        }).then((response)=>{
            if(response.ok){
                return response.json();
            }else{
                console.log("NetWork Error");
            }
        }).then((data)=>{
            if(data.status === 1){
                //注册成功处理逻辑
                window.alert('注册成功,欢迎登录!');
                window.location.href = 'signIn.html';
            }else{
                $("#messages").text("注册失败,请重试!");
                $("#messages").removeClass("invisible");
            }
        })
        .catch((error)=>{
            console.log(error);
        })
        .finally(()=>{
            console.log("处理完成");
        })

        //请求结束记得恢复注册按钮
        $("#signUp").text("注册");
        $("#signUp").removeClass("disabled");
    }
}


window.checkUsername = function checkUsername(username){
    console.log(username);

    if(username === ''){
        $("#messages").text("用户名不能为空!");
        $("#messages").removeClass("invisible");
        return false;
    }else{
        $("#messages").addClass("invisible");
    }

    let formDatas = new FormData();
    formDatas.append('username',username);

    //异步请求判重
    fetch(`http://localhost:8022/user/checkUsername`,{
        method:'POST',
        mode: 'cors',
        body:formDatas

    })
    .then((response)=>{
        if(response.ok){
            return response.json();
            
        }else{
            console.log("NetWork Error");
            
        }
    }).then((data)=>{
        if(data.status === 1){
            $("#messages").addClass("invisible");
            return true;
        }else{
            $("#messages").text("用户名已被使用!");
            $("#messages").removeClass("invisible");
            return false;
        }
    })
    .catch()
    .finally();

    return true;
}

window.checkPassword = function checkPassword(password){
    console.log(password);
    if(password === ''){
        $("#messages").text("密码不能为空!");
        $("#messages").removeClass("invisible");
        return false;
    }else{
        $("#messages").addClass("invisible");
        user_password = password;
        return true;
    }

}

window.checkRePassword = function checkRePassword(rePassword){
    console.log(rePassword);
    if(rePassword === '' || rePassword !== user_password){
        $("#messages").text("两次输入的密码不一致!");
        $("#messages").removeClass("invisible");
        return false;
    }else{
        $("#messages").addClass("invisible");
        return true;
    }
}